import axios from 'axios';
import FormData from 'form-data';
import fs from 'fs';
import path from 'path';

// 测试批量文件上传API
async function testBatchFileUpload() {
  console.log('=== 开始测试批量文件上传API ===');
  
  try {
    // 创建测试文件
    const testFiles = [
      { name: 'test1.jpg', content: 'test image 1' },
      { name: 'test2.jpg', content: 'test image 2' }
    ];
    
    const formData = new FormData();
    
    // 添加文件到FormData
    testFiles.forEach((file, index) => {
      const buffer = Buffer.from(file.content);
      formData.append('files', buffer, {
        filename: file.name,
        contentType: 'image/jpeg'
      });
    });
    
    console.log('📁 准备上传文件:');
    testFiles.forEach(file => {
      console.log(`   - ${file.name} (${file.content.length} bytes)`);
    });
    
    // 发送请求
    console.log('📤 发送请求到: /api/FileUpload/upload-batch');
    
    const response = await axios.post('http://localhost:5038/api/FileUpload/upload-batch', formData, {
      headers: {
        ...formData.getHeaders(),
        'Content-Type': 'multipart/form-data'
      },
      timeout: 30000
    });
    
    console.log('=== 📥 批量文件上传响应 ===');
    console.log('✅ HTTP状态码:', response.status);
    console.log('📋 响应数据:', JSON.stringify(response.data, null, 2));
    
    // 分析响应
    if (response.status === 200 && Array.isArray(response.data)) {
      console.log('✅ 批量上传成功！');
      
      response.data.forEach((result, index) => {
        if (result.success) {
          console.log(`📄 文件 ${index + 1}:`);
          console.log(`   - 状态: ${result.success ? '成功' : '失败'}`);
          console.log(`   - 消息: ${result.message}`);
          console.log(`   - URL: ${result.url}`);
          console.log(`   - 大小: ${result.size} bytes`);
          console.log(`   - 类型: ${result.contentType}`);
        } else {
          console.log(`❌ 文件 ${index + 1} 上传失败: ${result.message}`);
        }
      });
      
      return response.data;
    } else {
      console.error('❌ 批量上传失败');
      console.error('响应格式错误:', response.data);
      return null;
    }
    
  } catch (error) {
    console.error('=== 🚨 批量文件上传异常 ===');
    console.error('🔍 错误类型:', error.name);
    console.error('💬 错误消息:', error.message);
    
    if (error.response) {
      console.error('🌐 HTTP响应错误:');
      console.error('📊 状态码:', error.response.status);
      console.error('📋 响应数据:', error.response.data);
    }
    
    return null;
  }
}

// 运行测试
testBatchFileUpload().then(result => {
  if (result) {
    console.log('✅ 批量上传测试成功！');
    console.log('上传结果数量:', result.length);
  } else {
    console.log('❌ 批量上传测试失败');
  }
}).catch(error => {
  console.error('测试执行失败:', error);
}); 